# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: dibblethewrecker dibblethewrecker.at.jiwe.dot.org
# Contributor: William Rea <sillywilly@gmail.com>
# Contributor: Petri Moisio <petri.k.moisio@gmail.com>

_realname=gdal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.4
pkgrel=5
pkgdesc="A translator library for raster geospatial data formats (mingw-w64)"
arch=('any')
url="https://gdal.org/"
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-postgresql")
depends=("${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-crypto++"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libkml"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         #"${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xerces-c"
         "${MINGW_PACKAGE_PREFIX}-xz")
#optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql")
options=('strip' 'staticlibs')
source=(https://download.osgeo.org/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-external-qhull-static.patch)
sha256sums=('fc15d2b9107b250305a1e0bd8421dd9ec1ba7ac73421e4509267052995af5e83'
            '6952586cd4436003748e8d99f2e8e8a660ba59bdb3c0b294493695ef57f93077')

prepare() {
  cd ${srcdir}
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  
  sed -i "s|/usr/local|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|/usr|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|mandir='\${prefix}/man'|mandir='\${prefix}/share/man'|g" configure.ac
  for p in m4/*.m4
  do
    sed -i "s|/usr|${MINGW_PREFIX}|g" $p
  done
  # bug: http://osgeo-org.1560.x6.nabble.com/gdal-dev-jpeg2000-jasper-error-compiling-gdal-2-1-from-git-release-branch-td5299100.html
  sed -i -e 's@uchar@unsigned char@' frmts/jpeg2000/jpeg2000_vsil_io.cpp

  ./autogen.sh
}

build() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  CFLAGS+=" -fno-strict-aliasing"

  ./configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --with-poppler \
    --with-webp \
    --with-spatialite \
    --with-liblzma \
    --without-python \
    --without-perl

  sed -i GDALmake.opt -e "s|EXE_DEP_LIBS.*|EXE_DEP_LIBS = \$\(GDAL_SLIB\)|g"
  sed -i GNUmakefile -e "s|\$(GDAL_ROOT)\/||g"

  make
  # make man
}

package () {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  make DESTDIR="${pkgdir}" install-man

  # install license
  install -D -m644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
